#!/bin/bash
DATE=$(date "+%Y-%m-%d %H:%M:%S") ####显示打印日志的时间
BASH_PATH=$(cd `dirname $0`; pwd)
CURRENT_PATH=$(cd ${BASH_PATH}/../; pwd)
CONFIG_PATH="./autoUpdate.yaml"
LOG_FILE=${CURRENT_PATH}/log/git_update_$(date "+%Y%m%d%H%M%S").log

function LOG() {
    USER=$(whoami) ####那个用户在操作
    if [ ! -e ${CURRENT_PATH}/log ];then
       mkdir ${CURRENT_PATH}/log
    fi
    if [ ! -f ${LOG_FILE} ];then
       touch ${LOG_FILE}
    fi
    echo "[${DATE}] ${USER} [$1] $2"
    echo "[${DATE}] ${USER} [$1] $2" >> ${LOG_FILE}  ######（$0脚本本身，$@将参数作为整体传输调用, >> 将输出结果以追加的方式添加重定向到本地文件中）
}

function Code_Update() {
    LOG "INFO" "--start: $1 --------"
    if [ ! -d $1 ];then
        LOG "ERROR" "code dir not find, plesa check this path: $1"
        LOG "INFO" "end task"
        return
    fi

    cd $1
    git pull
    RET=$?
    if [ $RET -eq 0 ];then
        LOG "INFO" "git update success: $1"
    else
        LOG "ERROR" "git update fail: $1"
    fi
    LOG "INFO" "--end task-------"
    cd -
}

function Read_Yaml_Upade_Code(){
    key
    flag=0
    # 逐行读取内容
    cat $1 | while read LINE
    do
        if [ $flag == 0 ];then
            # 属性开始标志 vehicle_type:
            if [ "$(echo $LINE | grep ":")" != "" ];then
                # 截取出key值
                key=$(echo "$LINE" | awk -F " " '{print $2}')
                Code_Update "$key"
                continue
            else
                # 如果关键词后面没有空格，则跳出继续查找
                continue
            fi
        fi
    done
}
# Read_Yaml_Upade_Code $CONFIG_PATH

Code_Update "/volume3/SIT/开发/Python/Automatic_Tool/automatic-tool"
Code_Update "/volume3/SIT/开发/C++/Tahetis/Trunk/taheti"
Code_Update "/volume3/SIT/开发/Html/Silver_Star/sit"
Code_Update "/volume3/SIT/开发/C#/Robot_Code/Robot-Code"
Code_Update "/volume3/SIT/开发/C++/Pegasus/pegasus"
